#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include"mypushbutton.h"
#include<QPainter>
#include<QLabel>
#include<QFont>
#include"mycoin.h"
#include<QPropertyAnimation>
#include"dataconfig.h"
#include<QSound>

PlayScene::PlayScene(int levelNum){
    //QString str = QString("进去了第%1关").arg(levelNum);
    this->levelIndex = levelNum;
    //qDebug()<<str;
    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("翻金币场景");
    //创建菜单栏
    QMenuBar *bar = this->menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //创建按钮菜单项
    QAction *quitAction = startMenu->addAction("退出");
    //点击退出，退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //添加返回音效
    QSound* backSound = new QSound(":/res/BackButtonSound.wav",this);
    // 翻金币音效
    QSound* flipSound = new QSound(":/res/ConFlipSound.wav",this);

    //胜利按钮音效
    QSound* winSound = new QSound(":/res/LevelWinSound.wav",this);
    //创建返回按钮
    MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
    connect(closeBtn,&MyPushButton::clicked,[=](){
        backSound->play();
        //告诉主场景,我返回了
        emit this->playSceneBack();
    });

    //显示当前的关卡数
    QLabel *label = new QLabel(this);

    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Level: %1").arg(this->levelIndex);
    label->setText(str);
    label->move(100,500);
    label->setGeometry(30,this->height()-50,120,50);

    dataConfig config;
    //初始化每个关卡的二维数组
    for(int i = 0;i<4;++i)
    {
        for(int j = 0;j<4;j++)
        {
            this->gameArry[i][j]=config.mData[this->levelIndex][i][j];


        }
    }

    //胜利图片显示
    QLabel *winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());

    //创建金币的背景图
    for(int i = 0;i<4;++i){
        for(int j = 0;j<4;j++){
            QLabel *label = new QLabel(this);
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            label->move(57+i*50,200+j*50);
            if(this->gameArry[i][j]==1){
                //创建金币
                str = ":/res/Coin0001.png";
            }else{
                str = ":/res/Coin0008.png";
            }
            //创建金币
            MyCoin *coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);
            //给金币的属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArry[i][j];

            //将金币放入到金币的二维数组中，以便后期的维护
            coinBtn[i][j] = coin;
            //点击金币，进行反转（防手速太快的bug）
            connect(coin,&MyCoin::clicked,[=](){
                for(int i= 0;i<4;i++){
                    for(int j = 0;j<4;j++){
                        this->coinBtn[i][j]->isWin = true;
                    }
                }
            });

            connect(coin,&MyCoin::clicked,[=](){
               flipSound->play();

                coin->changeFlag();
               this->gameArry[i][j] = this->gameArry[i][j]==0?1:0;
                //
               QTimer::singleShot(300,this,[=](){
                   //翻转周围硬币
                   if(coin->posX + 1<=3)//右侧金币翻转的条件
                   {
                       coinBtn[coin->posX+1][coin->posY]->changeFlag();
                       this->gameArry[coin->posX+1][coin->posY] = this->gameArry[coin->posX+1][coin->posY]==0?1:0;
                   }
                   if(coin->posX - 1 >=0)//右侧金币翻转的条件
                   {
                       coinBtn[coin->posX-1][coin->posY]->changeFlag();
                       this->gameArry[coin->posX-1][coin->posY] = this->gameArry[coin->posX-1][coin->posY]==0?1:0;
                   }
                   if(coin->posY+ 1 <=3)//下侧金币翻转的条件
                   {
                       coinBtn[coin->posX][coin->posY+1]->changeFlag();
                       this->gameArry[coin->posX][coin->posY+1] = this->gameArry[coin->posX][coin->posY+1]==0?1:0;
                   }
                   if(coin->posY - 1 >=0)//上侧金币翻转的条件
                   {
                       coinBtn[coin->posX][coin->posY-1]->changeFlag();
                       this->gameArry[coin->posX][coin->posY-1] = this->gameArry[coin->posX][coin->posY-1]==0?1:0;
                   }
                   for(int i= 0;i<4;i++){
                       for(int j = 0;j<4;j++){
                           this->coinBtn[i][j]->isWin = false;
                       }
                   }

                   //判断是否胜利
                   this->isWin = true;
                   for(int i = 0;i<4;++i){
                       for(int j = 0;j<4;j++){
                           //只要有一个是反面，就失败
                           if(coinBtn[i][j]->flag == false){
                               this->isWin = false;
                               break;
                           }

                       }
                   }
                   if(this->isWin==true){
                       winSound->play();
                       qDebug()<<"胜利"<<endl;
                       for(int i = 0;i<4;++i){
                           for(int j = 0;j<4;j++){
                               coinBtn[i][j]->isWin = true;
                           }
                       }

                       //将胜利的图片移动下来
                       QPropertyAnimation* animation = new QPropertyAnimation(winLabel,"geometry");
                       animation->setDuration(1000);
                       //设置开始位置
                       animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));

                       //设置结束位置
                       animation->setEndValue(QRect(winLabel->x(),winLabel->y()+124,winLabel->width(),winLabel->height()));
                       //设置缓和曲线
                       animation->setEasingCurve(QEasingCurve::OutBounce);
                       //执行动画
                       animation->start();


                   }


               });

            });


        }
    }


}

void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
